package bawei.com.fragment;

import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;

import bawei.com.adapter.ZoneGridViewAdapter;


import bawei.com.bilibili.GameCentreActivity;
import bawei.com.bilibili.MyAppliction;
import bawei.com.bilibili.R;

import bawei.com.bilibili.RegionTypeDetailsActivity;
import bawei.com.bilibili.ZoneActivity;
import bawei.com.model.BaseCallback;

import bawei.com.model.Network;
import bawei.com.model.OkHttpHelper;
import bawei.com.model.bean.ReginBean;


/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/9 15:49
 * 邮箱:18310832074@163.com
 */

public
class
ZoneFragment extends Fragment implements AdapterView.OnItemClickListener {

    private GridView gv_fragment_zone;
    private ZoneGridViewAdapter zga;
    private List<ReginBean.DataBean> data;
    private int tid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.zonefragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        SopCastFragment.flag = false;
        gv_fragment_zone = (GridView) getActivity().findViewById(R.id.gv_fragment_zone);

        zga = new ZoneGridViewAdapter(getActivity());

        String str = initAssets();

        Gson gson = new Gson();

        ReginBean reginBean = gson.fromJson(str, ReginBean.class);

        data = reginBean.getData();

        zga.addRest(data);

        gv_fragment_zone.setAdapter(zga);

        gv_fragment_zone.setOnItemClickListener(this);

    }

    private String initAssets() {

        StringBuilder stringBuilder = new StringBuilder();

        AssetManager assets = getActivity().getAssets();

        try {

            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assets.open("region.json")));

            String line;

            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


        for (int j = 0; j < data.size(); j++) {

            tid = data.get(i).getTid();

        }
        if (data.get(i).getName().equals("直播")) {
            Intent intent = new Intent(getActivity(), ZoneActivity.class);
            intent.putExtra("tid", tid);
            startActivity(intent);
        }else
        if(data.get(i).getName().equals("游戏中心")){
            Intent intent = new Intent(getActivity(), GameCentreActivity.class);
            startActivity(intent);
        }else {

            Intent intent = new Intent(getActivity(), RegionTypeDetailsActivity.class);
            intent.putExtra("name",data.get(i).getName());
            startActivity(intent);

        }


    }

    private void initData(int tid) {

        String url = Network.PARTITION_HEAD + Network.PARTITION_RECOMMEND + tid;

        OkHttpHelper.getinstance().get(url, new BaseCallback() {
            @Override
            public void onRequestBefore() {

            }
            @Override
            public void onFailure(Request request, Exception e) {

            }
            @Override
            public void onSuccess(Response response, Object o) {

            }
            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });

    }
}
